package cn.gson.crm.controller.relation;



import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.dao.ImagesDao;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.dao.StudentJourneyDao;
import cn.gson.crm.model.entity.BedRoom;
import cn.gson.crm.model.entity.Classes;
import cn.gson.crm.model.entity.Cuikuan;
import cn.gson.crm.model.entity.Emp;
import cn.gson.crm.model.entity.Images;
import cn.gson.crm.model.entity.Payrecord;
import cn.gson.crm.model.entity.PayrecordDetail;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.entity.User;
import cn.gson.crm.model.entity.WhStudentJourney;
import cn.gson.crm.model.mapper.StudentJourneyMapper;
import lombok.extern.log4j.Log4j;

@Controller
@RequestMapping("/journey")
@Log4j
public class StudentJourneyController {
	
	
	@Autowired
	private StudentJourneyDao sjd;
	
	@Autowired
	private StudentDao stud;
	
	@Autowired
	private StudentJourneyMapper sjm;
	
	/**
	 * 图片表
	 */
	@Autowired
	private ImagesDao imgDao ;
	
	/**
	 * 查询所有学员的就业反馈信息
	 * @param stuinfor
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> index(
			Student stu, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize){
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<Student> pageData = sjm.selectAllJourneyStudent(stu);

		PageInfo<Student> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	/**
	 * 根据学员id查询学员历程时间轴
	 * @param model
	 * @return
	 */
	@GetMapping("journey_details/{id}")
	public String journey_details(
			@PathVariable(name = "id", required = false) 
			Long id,
			Model model) {
			List<WhStudentJourney> wsj = sjm.selectJourneylist(id);
			Student stu = stud.findOne(id);
			model.addAttribute("student",stu);
			
			model.addAttribute("journey", wsj);
			return "journey/student_journey";
	}

	/**
	 * 打开添加历程的界面
	 * @param student
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("addjourney/{id}")
	public String addsutdent(@PathVariable(name = "id", required = false) Long id,Model model) {
		
		Student stu = stud.findOne(id);
		model.addAttribute("student",stu);
		return "journey/addjourney";
	}

	
	@Value("${upload.dir}")
	private String uploadDir;
	/**
	 * 保存学员历程内容
	 */
	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String,Object> detailSave(
			@Valid WhStudentJourney wsj,
			BindingResult br,
			@RequestParam(defaultValue="public") String type,
			@RequestParam("file") MultipartFile file,
			HttpServletRequest request) throws IllegalStateException, IOException {
	
		Map<String,Object> obj = new HashMap<String, Object>();
		//设置路径
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		String dateDir = sdf.format(new Date());
		//获取文件后缀名
		String ext = FilenameUtils.getExtension(file.getOriginalFilename());
		String newFileName = UUID.randomUUID().toString()+"."+ext;
		String filePath = String .format("%s/%s/%s",System.getProperty("user.dir") + uploadDir,type,dateDir);
		File fileDir = new File(filePath);
		System.out.println(fileDir);
		if(!fileDir.exists()){
			// 如果目录不存在，则创建出来
			fileDir.mkdirs();
		}
		Images img = new Images();
		img.setDelete(true);
		img.setImageType(file.getContentType());
		img.setModeType(type);
		img.setName(file.getOriginalFilename());
		img.setPath(String.format("%s/%s/%s", type,dateDir,newFileName));
		img.setSize(file.getSize());
		img.setSuffix(ext);
		img.setUploaddate(new Date());
		//将图片路径保存到数据库
		imgDao.save(img);
		file.transferTo(new File(fileDir, newFileName));
		obj.put("success", false);

			wsj.setProcessImage(img);
			sjd.save(wsj);
			Map<String, Object> saveParams = new HashMap<>();
			saveParams.put("id", wsj.getStudent().getId());
			saveParams.put("journeyId",wsj.getId());
			sjm.saveOneStudentJourney(saveParams);
			obj.put("success", true);
		return obj;
	}
	
	
	
}
